package com.cytech.cloudclinic.moudle.main.clinic.admission.prescription.template;

import com.cytech.cloudclinic.base.BaseBean;
import com.cytech.cloudclinic.base.BaseObserver;
import com.cytech.cloudclinic.base.BasePresenter;
import com.cytech.cloudclinic.bean.Illness;
import com.cytech.cloudclinic.bean.PrescriptionTag;
import com.cytech.cloudclinic.bean.RespPrescriptionDrug;
import com.yechaoa.yutils.ToastUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

import okhttp3.MediaType;
import okhttp3.RequestBody;

public class PrescriptionTemplatePresenter extends BasePresenter<PrescriptionTemplateView> {
    public PrescriptionTemplatePresenter(PrescriptionTemplateView baseView) {
        super(baseView);
    }

    public void getPrescriptionTags(int drugParentTag) {
        JSONObject json = new JSONObject();
        try {
            json.put("drug_tag",drugParentTag);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        String requestStr = json.toString();
        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), requestStr);
        addDisposable(apiServer.getPrescriptionTags(body), new BaseObserver<BaseBean<List<PrescriptionTag>>>(baseView, true) {

            @Override
            public void onSuccess(BaseBean<List<PrescriptionTag>> o) {
                baseView.getPrescriptionSuccess(o.result);
            }

            @Override
            public void onError(String msg) {
                ToastUtil.showToast(msg);
            }
        });
    }

    public void getPrescriptionItem(String prescription_id) {
        JSONObject json = new JSONObject();
        try {
            json.put("prescription_id",prescription_id);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        String requestStr = json.toString();
        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), requestStr);
        addDisposable(apiServer.getPrescriptionDrug(body), new BaseObserver<BaseBean<RespPrescriptionDrug>>(baseView, false) {

            @Override
            public void onSuccess(BaseBean<RespPrescriptionDrug> o) {
                baseView.getPrescriptionDrugSuccess(o.result.data.drugs);
            }

            @Override
            public void onError(String msg) {
                ToastUtil.showToast(msg);
            }
        });
    }
}
